package edu.calpoly.android.privatecontacts;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

public class TeacherActivity extends Activity {

   private List<ParseObject> entries;
   private List<TeacherEntry> realEntries;
   private String table;
   private String teacherId;
   //private EntryAdapter m_adapter;
   //private ListView entryView;
   ListView list;
   
   //view stuff
   private TextView name;
   private TextView grade;
   private Button phone;
   private TextView email;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      this.setContentView(R.layout.activity_teacher);

      Bundle content = this.getIntent().getExtras();
      table = content.getString("table");
      teacherId = content.getString("id");
      this.realEntries = new ArrayList<TeacherEntry>();
      this.entries = new ArrayList<ParseObject>();
      
      //-- things for view
      this.name = (TextView) this.findViewById(R.id.teacherName);
      this.grade = (TextView) this.findViewById(R.id.teacherGrade);
      this.phone = (Button) this.findViewById(R.id.teacherPhone);
      this.email = (TextView) this.findViewById(R.id.teacherEmail);
      
      this.phone.setOnClickListener(new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent goCall = new Intent(Intent.ACTION_DIAL);
			goCall.setData(Uri.parse("tel:"+realEntries.get(0).getPhone()));
			startActivity(goCall);
		}
    	  
      });
      
      new RemoteDataTask().execute();
      
   }

   private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
      // Override this method to do custom remote calls
      protected Void doInBackground(Void... params) {
         // Gets the current list of todos in sorted order
         ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(table);
         Log.d("prototype", "query state: " + (query == null));
         try {
            query.get(teacherId);
            entries = query.find();
         } catch (ParseException e) {
            Log.d("prototype", "problem initializing entries");
         }
         return null;
      }

      @Override
      protected void onPreExecute() {
         // ListActivity.this.progressDialog =
         // ProgressDialog.show(ListActivity.this, "", "Loading...", true);
         super.onPreExecute();
      }

      @Override
      protected void onProgressUpdate(Void... values) {

         super.onProgressUpdate(values);
      }

      @Override
      protected void onPostExecute(Void result) {
         Log.d("proto", "in postExecute");
         for (ParseObject entry : entries) {
            realEntries.add(new TeacherEntry((String)entry.get("firstName"), 
                  (String)entry.get("lastName"), (String)entry.get("grade"), 
                  (String)entry.get("phoneNumber"), (String)entry.get("email")));
         }
         TeacherEntry teacher = realEntries.get(0);
         name.setText("Student: " + teacher.toString());
         grade.setText("Grade: " + teacher.getGrade());
         phone.setText("Phone Number: " + teacher.getPhone());
         email.setText("email: " + teacher.getEmail());
         Log.d("proto", "finished postExecute");
      }
   }
}
